package just.stuff.morsels;

import java.io.File;
import java.io.IOException;

import just.stuff.morsels.data.BerkeleyDataSource;
import just.stuff.morsels.ui.label.LabelPage;
import just.stuff.morsels.ui.morsel.MorselPage;
import just.stuff.morsels.ui.search.SearchResults;
import just.stuff.morsels.ui.user.LoginCallbackHandler;
import just.stuff.morsels.ui.user.UserPage;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.coding.MixedParamUrlCodingStrategy;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see wicket.myproject.Start#main(String[])
 */
public class MorselApplication extends WebApplication {

    private static final String DEFAULT_LUCENE_DIR = "morseldb.index";
	private static final String DEFAULT_DB_DIR = "morseldb";
	private static final String LUCENE_DIR_PARAM = "luceneDir";
	private static final String DB_DIR_PARAM = "dbDir";
	
	/**
	 * Database connector.
	 */
	private BerkeleyDataSource dataSource;

    /**
     * Constructor
     * @throws IOException 
     * @throws LockObtainFailedException 
     * @throws CorruptIndexException 
     * @throws ParseException 
     * @throws SourceException 
     */
	public MorselApplication() throws CorruptIndexException, LockObtainFailedException, IOException, ParseException
	{
		File dbDir = new File( System.getProperty( DB_DIR_PARAM, System.getProperty( "user.home")+File.separator+DEFAULT_DB_DIR));
		if( !dbDir.exists() )
			dbDir.mkdirs();
		File luceneDir = new File( System.getProperty( LUCENE_DIR_PARAM, System.getProperty( "user.home")+File.separator+DEFAULT_LUCENE_DIR));
		if( !luceneDir.exists() )
			luceneDir.mkdirs();

		this.dataSource = new BerkeleyDataSource( dbDir, luceneDir );
	}
	
	@Override
	protected void init() {
		super.init();
		mount( new MixedParamUrlCodingStrategy( "/search", SearchResults.class, new String[] {} ) );
		mount( new MixedParamUrlCodingStrategy( "/morsel", MorselPage.class , new String[] { MorselPage.ID_PARAM, MorselPage.EDIT_PARAM}));
		mount( new MixedParamUrlCodingStrategy( "/label", LabelPage.class , new String[] { "label"}));
		mount( new MixedParamUrlCodingStrategy( "/user", UserPage.class , new String[] {}));
		mount( new LoginCallbackHandler("login"));
	}
	
	@Override
	public Session newSession(Request request, Response response) {
		return new MorselSession( request );
	}
	
	/**
	 * @see wicket.Application#getHomePage()
	 */
	public Class<? extends WebPage> getHomePage()
	{
		return MorselPage.class;
	}
	
	/**
	 * Return type narrowing static getter override.
	 * @return
	 */
	public static MorselApplication get() {
		return (MorselApplication) WebApplication.get();
	}
	
	public static BerkeleyDataSource dataSource() {
		return get().dataSource;
	}
	
}
